package interfaces.loading;

import fileHandling.language.LanguageLoader;
import fileHandling.language.interfaces.MenuText;
import fileHandling.language.interfaces.NewGameText;
import fileHandling.language.options.Filters;
import interfaces.GUISource;
import interfaces.ImageUpdater;
import interfaces.WordWrapHelper;
import interfaces.superWidgets.InterfaceLabel;
import interfaces.superWidgets.KeyValueContent;

import logic.common.game.GameMode;
import main.GameCycle;
import map.Map;

import org.fenggui.binding.render.ImageFont;
import org.fenggui.layout.StaticLayout;

public class MapInfoWindow extends LoadingWindow {
	
	private ImageUpdater mapPreview;
	private ImageFont labelFont;
	private int border;
	private InterfaceLabel descrLabel;
	private GameCycle gameCycle;
	private KeyValueContent mapInfoContent;
	
	public MapInfoWindow(Map map, GameCycle gameCycle, int width, int height) {
		super(map, map.getMapName());
		this.gameCycle = gameCycle;
		
		setSize(width, height);
		
		labelFont = GUISource.middleFont;
		
		content.setLayoutManager(new StaticLayout());
		layout();
		
		init();
		createMatchInfos();
		
		layout();
	}
	
	private void init() {
		border = content.getHeight() / 40;
		
		int previewHeight = content.getHeight() - (2 * border);
		
		String previewPath = map.getMapPath();
		mapPreview = new ImageUpdater(previewPath, border, border, previewHeight, previewHeight);
		content.addWidget(mapPreview);
		
		int fontHeight = labelFont.getHeight();
		int labelX = mapPreview.getX() + mapPreview.getWidth() + border;
		int labelWidth = content.getWidth() - labelX - border;
		
		String descrString = map.getMapDescription().getDescriptionString();
		descrLabel = new InterfaceLabel(labelFont);
		descrLabel.setSize(labelWidth, fontHeight);
		descrLabel.setX(labelX);
		WordWrapHelper.wrapWords(descrLabel, descrString);
		descrLabel.setY(content.getHeight() - border - descrLabel.getHeight());
		content.addWidget(descrLabel);
	}
	
	private void createMatchInfos() {
		int x = mapPreview.getX() + mapPreview.getWidth() + border;
		int y = border;
		int width = content.getWidth() - x - border;
		int height = descrLabel.getY() - border;
		mapInfoContent = new KeyValueContent(width, height, labelFont, false);
		mapInfoContent.setXY(x, y);
		content.addWidget(mapInfoContent);
		
		String left = LanguageLoader.get(NewGameText.Balanced_Teams);
		String right = LanguageLoader.get(MenuText.getYesNo(gameCycle.hasBalancedTeams()));
		mapInfoContent.addLine(left, right);
		
		left = LanguageLoader.get(Filters.Friendly_Fire);
		right = LanguageLoader.get(MenuText.getYesNo(gameCycle.hasFriendlyFire()));
		mapInfoContent.addLine(left, right);
		
		left = LanguageLoader.get(NewGameText.Respawn_Time);
		right = gameCycle.getRespawnTime() + "";
		mapInfoContent.addLine(left, right);
		
		left = LanguageLoader.get(NewGameText.Max_Players);
		right = gameCycle.getMaxPlayers() + "";
		mapInfoContent.addLine(left, right);
		
		GameMode mode = gameCycle.getGameMode();
		if(mode == GameMode.Conquest) {
			left = LanguageLoader.get(NewGameText.Tickets);
			right = gameCycle.getTickets() + "";
			mapInfoContent.addLine(left, right);
		} else if(mode == GameMode.TeamDeathmatch) {
			left = LanguageLoader.get(NewGameText.Score_Limit);
			right = gameCycle.getScoreLimit() + "";
			mapInfoContent.addLine(left, right);
		}
	}
}